'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 公会搜索
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../../consts/code');
var unionGroupService = require('../../../../services/unionGroupService');
var unionGroupModel = require('../../../../models/unionGroupModel.js');
var unionGroupConf = require('../../../../config/unionGroupConf.js');

/**
 * 公会搜索
 */
module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var name_key_word = msg.name_key_word;
    var union_list = [];
    var union_id = 0;
    async.waterfall(
        [
            function (cb) {
                unionGroupService.getUnionGroupList(cb);
            },
            function (union_list_all, cb) {
                if (!!name_key_word) {
                    _.some(union_list_all, function (union_item) {
                        if (union_item.name === name_key_word) {
                            union_id = union_item.union_id;
                            union_list = [union_item];
                            return true;
                        }
                    });
                }
                if (union_list.length < 1) {
                    return next(null, {
                        code: Code.UNION_IS_NOT_EXIST
                    });
                }
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    union_list: union_list
                }
            });
        }
    );
};
